// pluginDialog.cpp (c) 2011 JV Software
//

#include "FallenPlugin.h"
#include <Uxtheme.h>

FArray<FPluginDialog *>		dialogs;

LRESULT CALLBACK FPluginDialog::PluginDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
		return TRUE;

		case WM_PAINT:
			return FALSE;

		case WM_CLOSE:
		case WM_QUIT:
			ShowWindow( hDlg, SW_HIDE );
			return TRUE;
		case WM_COMMAND:
			FPluginDialog::FindDialog( hDlg )->OnCommand( LOWORD(wParam) );
			return TRUE;
		break;
	}
	return FALSE;
}

/*
=============
FPluginDialog::OnCommand
=============
*/
void FPluginDialog::OnCommand( int cmd ) {

}

/*
=============
FPluginDialog::FindDialog
=============
*/
FPluginDialog* FPluginDialog::FindDialog( HWND hwnd ) {
	for(int i = 0; i < dialogs.size(); i++ ) {
		if(dialogs[i]->dialogHwnd == hwnd) {
			return dialogs[i];
		}
	}

	return NULL;
}

/*
=============
FPluginDialog::RegisterDialog
=============
*/
void FPluginDialog::RegisterDialog( const FPluginDialog *dialog ) {
	dialogs.push_back( (FPluginDialog *)dialog );
}

/*
=============
FPluginDialog::RegisterDialog
=============
*/
void FPluginDialog::DestroyAllDialogs( void ) {
	for(int i = 0; i < dialogs.size(); i++ ) {
		DestroyWindow( dialogs[i]->dialogHwnd );
		delete dialogs[i];
	}

	dialogs.clear();
}

/*
=============
FPluginDialog::AddMenu
=============
*/
void FPluginDialog::AddMenu( int menuId ) {
	HRSRC res = FindResource(GetLocalModuleHandle(), MAKEINTRESOURCE(menuId), RT_MENU );

	if(!res) {
		MGlobal::displayError( "Failed to load dialog menu. " );
		return;
	}

	//hMenu = (HMENU)LoadResource(GetLocalModuleHandle(),res);
	//if(!hMenu) {
	//	MGlobal::displayError( "Failed to load dialog res menu. " );
	//	return;
	//}

	hMenu = LoadMenu( GetLocalModuleHandle(), MAKEINTRESOURCE( menuId ) );
	if(!hMenu) {
		MGlobal::displayError( "Failed to load dialog res menu. " );
		return;
	}

	SetMenu(dialogHwnd,hMenu );
}

/*
=============
FPluginDialog::EnableControl
=============
*/
void FPluginDialog::EnableControl( int res ) {
	HWND hwnd  = GetDlgItem (dialogHwnd, res);
	EnableWindow(hwnd, true);
}

/*
=============
FPluginDialog::GetSelectionFromControl
=============
*/
int FPluginDialog::GetSelectionFromControl( int res ) {
	HWND hwnd  = GetDlgItem (dialogHwnd, res);
	return (int)SendMessage(hwnd, CB_GETCURSEL, 0, 0);
}

/*
=============
FPluginDialog::PopulateComboBox
=============
*/
void FPluginDialog::PopulateComboBox( int res, int numItems, FString *str ) {
	HWND hwnd  = GetDlgItem (dialogHwnd, res);

	for(int i=0; i<numItems; i++) {
        SendMessage(hwnd, CB_ADDSTRING, 0, (LPARAM)str[i].c_str() );
	}

	SendMessage(hwnd, CB_SETCURSEL, 0, 0);
}

/*
=============
FPluginDialog::Init
=============
*/
void FPluginDialog::Init( int resourceId ) {
	HRSRC res = FindResource(GetLocalModuleHandle(), MAKEINTRESOURCE(resourceId), RT_DIALOG );
	M3dView view = M3dView::active3dView();
	HWND parentHwnd = view.applicationShell();

	if(!res) {
		MGlobal::displayError( "Failed to load dialog resource. " );
		return;
	}

	dialogHandle = LoadResource( GetLocalModuleHandle(), res );

	if(dialogHandle == NULL) {
		MGlobal::displayError( "Failed to load dialog" );
		return;
	}

	pdlgTemplate = (LPCDLGTEMPLATE) LockResource(dialogHandle);
	dialogHwnd = CreateDialogIndirect(MhInstPlugin, pdlgTemplate, parentHwnd, (DLGPROC)FPluginDialog::PluginDialogProc);

	RegisterDialog( this );
	SetWindowTheme(dialogHwnd, L"Explorer", NULL);
}

/*
=============
FPluginDialog::Show
=============
*/
void FPluginDialog::Show( void ) {
	ShowWindow( dialogHwnd, SW_SHOW );
}

/*
=============
FPluginDialog::Hide
=============
*/
void FPluginDialog::Hide( void ) {

}